package com.example.schedulemanager.utils;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import androidx.core.app.NotificationCompat;

import com.example.schedulemanager.R;
import com.example.schedulemanager.model.Schedule;
import com.example.schedulemanager.repository.ScheduleRepository;
import com.example.schedulemanager.view.MainActivity;
import com.example.schedulemanager.view.ReminderActivity;

import java.util.List;

public class ScheduleNotificationService extends Service {
    
    private static final String CHANNEL_ID = "schedule_reminder";
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        createNotificationChannel();

        int scheduleId = intent.getIntExtra("schedule_id", -1);
        String title = intent.getStringExtra("schedule_title");
        String description = intent.getStringExtra("schedule_description");

        if (scheduleId != -1 && title != null) {
            showNotification(scheduleId, title, description);
        }
        
        // Mark the task as completed in the database.
        // This is an example, you might want a more complex logic.
        if (scheduleId != -1) {
            ScheduleRepository repository = new ScheduleRepository(getApplication());
            repository.getScheduleById(scheduleId, schedule -> {
                if (schedule != null) {
                    schedule.setCompleted(true);
                    repository.update(schedule);
                }
                // Stop the service once the work is done.
                stopSelf(startId);
            });
        } else {
            stopSelf(startId);
        }

        return START_NOT_STICKY;
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "日程提醒",
                    NotificationManager.IMPORTANCE_HIGH
            );
            channel.setDescription("日程管理应用的提醒通知");
            
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
    
    private void showNotification(int scheduleId, String title, String description) {
        // Intent to open MainActivity when notification is tapped
        Intent mainIntent = new Intent(this, MainActivity.class);
        mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent contentPendingIntent = PendingIntent.getActivity(
                this, scheduleId, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        // Intent for the full-screen reminder
        Intent fullScreenIntent = new Intent(this, ReminderActivity.class);
        fullScreenIntent.putExtra("schedule_title", title);
        fullScreenIntent.putExtra("schedule_description", description);
        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(
                this, scheduleId, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );
        
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("日程提醒: " + title)
                .setContentText(description)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .setContentIntent(contentPendingIntent)
                .setFullScreenIntent(fullScreenPendingIntent, true)
                .setAutoCancel(true);
        
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.notify(scheduleId, builder.build());
    }
} 